package com.dex4d.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.dex4d.R;

/**
 * 自定义Dialog
 *
 * @author chengyuchun
 */
public class CustomDialog extends Dialog {
    @SuppressWarnings("unused")
    private Context mContext;
    protected CustomDialogOnClickListener mClickListener;
    private int mDialogView;
    private Button btn_ok,btn_cancel;

    protected Button btnLeft, btnRight;

    public CustomDialog(Context context, int layoutId) {
        this(context, layoutId, null);
    }


    public CustomDialog(Context context, int layoutId,
                        CustomDialogOnClickListener clickListener) {
        this(context, layoutId, R.style.dialog, clickListener);
    }

    public CustomDialog(Context context, int layoutId, int dialogStyle
    ) {
        super(context, dialogStyle);
        mContext = context;
        mDialogView = layoutId;
    }

    public CustomDialog(Context context, int layoutId, int dialogStyle,
                        CustomDialogOnClickListener clickListener) {
        super(context, dialogStyle);
        mContext = context;
        mClickListener = clickListener;
        mDialogView = layoutId;
    }

    /**
     * 调用者自己去处理按钮的点击事件
     *
     * @author chengyuchun
     */
    public interface CustomDialogOnClickListener {
        //注意 false是确定，true是取消
        void click(boolean flag);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(mDialogView);

        btnLeft = (Button) findViewById(R.id.dialog_btn_left);
        btnRight = (Button) findViewById(R.id.dialog_btn_right);

        //!!!!!  true-取消  false-确定
        btnRight.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (mClickListener != null) {
                    mClickListener.click(false);
                }
                CustomDialog.this.dismiss();
            }
        });
        btnLeft.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mClickListener != null) {
                    mClickListener.click(true);
                }
                CustomDialog.this.dismiss();
            }
        });
    }

    public void setClickListener(CustomDialogOnClickListener listener) {
        this.mClickListener = listener;
    }

    public void setBtnOkText(String text){
        btn_ok.setText(text);
    }

    public void setBtnCancelText(String text){
        btn_cancel.setText(text);
    }
}
